﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySqlX.XDevAPI;
using PropertyManagementSystemAPI.Models;
using System.Xml.Linq;

namespace PropertyManagementSystemAPI.Controllers.Owner
{
    /// <summary>
    /// 业主房产管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    
    public class Property_managementController : ControllerBase
    {
        public readonly IFreeSql db;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public Property_managementController(IFreeSql db)
        {
            this.db = db;
        }
        /// <summary>
        /// 业主房产管理查询
        /// </summary>
        /// <param name="ys"></param>
        /// <param name="ym"></param>
        /// <param name="name"></param>
        /// <param name="pCellId"></param>
        /// <returns></returns>
        [HttpGet]
        public object Search(int ys, int ym, string? name, int pCellId)
        {
            int pageSize = ys; //页数
            int pageIndex = ym;//页码
            string str = HttpContext.Session.GetString("user");
            var ww = db.Select<PropertyInformation, CellInformation>()
                       .LeftJoin((a, b) => a.PCellId == b.CellId)
                       .WhereIf(!string.IsNullOrEmpty(name), (a, b) => b.CellName == name)
                       .Where((a, b) => a.PCellId == Convert.ToInt32(str))
                       .ToList((a, b) => new { a.PId, a.PCellId, a.PRidgepole, a.PCount, a.PUnitFloor, a.PUserId, a.PStayTime, b.CellName, b.CellId });

            int totalCount = ww.Count();//总条数
            var list = ww.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet("La")]
        public object La()
        {
            string str = HttpContext.Session.GetString("user");
            var zht = db.Select<CellInformation>().
                ToList
                (c => new { c.CellId, c.CellName });
            return zht;
        }
        /// <summary>
        /// 根据小区ID查询房产信息
        /// </summary>
        /// <param name="pCellId">小区ID</param>
        /// <returns>房产信息</returns>
        [HttpGet("GetByCellId")]
        public object GetByCellId(int pCellId)
        {
            string str = HttpContext.Session.GetString("user");
            var list = db.Select<PropertyInformation, CellInformation>()
                .LeftJoin((a, b) => a.PCellId == b.CellId)
                .Where((a, b) => a.PCellId == pCellId&&a.PUserId== Convert.ToInt32(str))
                .ToList((a, b) => new { a.PId, a.PCellId, a.PRidgepole, a.PCount, a.PUnitFloor, a.PUserId, a.PStayTime, b.CellName, b.CellId });
            int totalCount = list.Count();//总条数
            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="ww"></param>
        /// <returns></returns>
        [HttpPost]
        public int Add(PropertyInformation ww)
        {
            var infos = new PropertyInformation()
            {
                PId = ww.PId,
                PCellId = ww.PCellId,
                PRidgepole = ww.PRidgepole,
                PCount = ww.PCount,
                PUnitFloor = ww.PUnitFloor,
                PUserId = ww.PUserId,
                PStayTime = DateTime.Now,
            };
            var q = db.Insert(infos).ExecuteAffrows();
            return q;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ww"></param>
        /// <returns></returns>
        [HttpPut]
        public int Edit(int id, PropertyInformation ww)
        {
            var mm = db.Update<PropertyInformation>().Set(a => new PropertyInformation
            {
                PId = ww.PId,
                PCellId = ww.PCellId,
                PRidgepole = ww.PRidgepole,
                PCount = ww.PCount,
                PUnitFloor = ww.PUnitFloor,
                PUserId = ww.PUserId,
                PStayTime = ww.PStayTime,
            }).Where(a => a.PId == id).ExecuteAffrows();
            return mm;
        }
    }
}
